package com.haitaoit.chengquan.utils;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.center.adapter.order.ApplyReturnAdapter;
import com.haitaoit.chengquan.module.home.adapter.parts.PartsSpecAdapter;
import com.haitaoit.chengquan.module.home.model.PartsDetailModel;
import com.haitaoit.chengquan.module.home.model.TitleModel;
import com.haitaoit.chengquan.view.HorizontalListView;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

public class DialogUtil {
    /**
     * 单个 确定提示dialgo(确定) showInfoDialog(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @param context
     * @return Dialog
     * @since 1.0.0
     */
    public static Dialog showInfoDialog(final Context context, String titel, String content) {
        final Dialog dlg = new Dialog(context, R.style.noDialogTheme);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.dialog_show_info, null);
        TextView title = (TextView) layout.findViewById(R.id.title);
        title.setText(titel);
        TextView cont = (TextView) layout.findViewById(R.id.content);
        cont.setText(content);

        layout.findViewById(R.id.confirm).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dlg.onWindowAttributesChanged(lp);
        dlg.setCanceledOnTouchOutside(true);
        dlg.setContentView(layout);
        dlg.setCancelable(false);
        return dlg;
    }

    /**
     * 两个按键（确定，取消）的dialog showInfoDialog1(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @param context
     * @param call
     * @return Dialog
     * @since 1.0.0
     */
    public static Dialog showInfoDialog1(final Context context, String titel,
                                         String content, String conf, String canc, final BackCall call) {
        final Dialog dlg = new Dialog(context, R.style.PushUpInDialogThem);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.dialog_show_info1, null);
        TextView title = (TextView) layout.findViewById(R.id.title);
        title.setText(titel);
        TextView cont = (TextView) layout.findViewById(R.id.content);
        cont.setText(content);
        TextView confirm = (TextView) layout.findViewById(R.id.confirm);
        confirm.setText(conf);
        TextView cancel = (TextView) layout.findViewById(R.id.cancel);
        cancel.setText(canc);

        layout.findViewById(R.id.confirm).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.confirm, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });
        layout.findViewById(R.id.cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.cancel, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });
        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dlg.onWindowAttributesChanged(lp);
        dlg.setCanceledOnTouchOutside(true);
        dlg.setContentView(layout);
        dlg.setCancelable(false);
        return dlg;
    }

    // 选择服务商
    public static Dialog showInfoDialog2(final Context context,
                                         String content, String conf, String canc, final BackCall call) {
        final Dialog dlg = new Dialog(context, R.style.PushUpInDialogThem);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.dialog_show_info1, null);
        TextView title = (TextView) layout.findViewById(R.id.title);
        title.setVisibility(View.GONE);
        TextView cont = (TextView) layout.findViewById(R.id.content);
        cont.setText(content);
        cont.setGravity(Gravity.TOP);

        TextView confirm = (TextView) layout.findViewById(R.id.confirm);
        confirm.setText(conf);
        if (conf.equals("") && conf.length() < 1) {
            confirm.setVisibility(View.GONE);
        }
        TextView cancel = (TextView) layout.findViewById(R.id.cancel);
        cancel.setText(canc);
        cancel.setTextColor(context.getResources().getColor(R.color.app_color));

        layout.findViewById(R.id.confirm).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.confirm, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });
        layout.findViewById(R.id.cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.cancel, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });
        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dlg.onWindowAttributesChanged(lp);
        dlg.setCanceledOnTouchOutside(true);
        dlg.setContentView(layout);
        dlg.setCancelable(false);
        return dlg;
    }

    // 图片 拍照
    public static Dialog ShowPhotoDialog(Context context, final BackCall call) {
        final Dialog dlg = new Dialog(context, R.style.PushUpInDialogThem);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.dialog_camero_show, null);

        TextView tv_photos = (TextView) layout.findViewById(R.id.tv_photos);
        TextView tv_camera = (TextView) layout.findViewById(R.id.tv_camera);
        TextView tv_dismiss = (TextView) layout.findViewById(R.id.tv_dismiss);

        tv_photos.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.tv_photos, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });

        tv_camera.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.tv_camera, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });

        tv_dismiss.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        dlg.onWindowAttributesChanged(lp);
        dlg.setContentView(layout);
        dlg.setCancelable(false);
        return dlg;
    }

    // 图片 拍照
    public static Dialog ShowTop(Context context, final BackCall call) {
        final Dialog dlg = new Dialog(context, R.style.noDialogTheme);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.dialog_camero_show, null);

        TextView tv_photos = (TextView) layout.findViewById(R.id.tv_photos);
        tv_photos.setText("替\t换");
        TextView tv_camera = (TextView) layout.findViewById(R.id.tv_camera);
        tv_camera.setText("删\t除");
        TextView tv_dismiss = (TextView) layout.findViewById(R.id.tv_dismiss);
        tv_dismiss.setText("取\t消");
        tv_photos.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.tv_photos, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });

        tv_camera.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.tv_camera, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });

        tv_dismiss.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        dlg.onWindowAttributesChanged(lp);

        dlg.setCanceledOnTouchOutside(true);
        dlg.setContentView(layout);
        dlg.setCancelable(false);
        return dlg;
    }

    // 收藏
    public static Dialog ShowCollectDialog(Context context, final BackCall call) {
        final Dialog dlg = new Dialog(context, R.style.PushUpInDialogThem);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.dialog_collect_show, null);

        layout.findViewById(R.id.collect_add_ll).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.collect_add_ll, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });

        layout.findViewById(R.id.tv_dismiss).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        dlg.onWindowAttributesChanged(lp);
        dlg.setContentView(layout);
        dlg.setCancelable(false);
        return dlg;
    }

    // 退货原因
    public static Dialog ShowReturnDialog(final Context context, List<TitleModel> list, final BackCall call) {
        final Dialog dlg = new Dialog(context, R.style.noDialogTheme);
        LayoutInflater inflater = (LayoutInflater) context.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_return_show, null);
        ListView return_list = (ListView) layout.findViewById(R.id.return_list);

        final int[] index = {0};
        final ApplyReturnAdapter adapter = new ApplyReturnAdapter(context);
        adapter.setChecked(0);
        adapter.setList(list);
        return_list.setAdapter(adapter);
        return_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                index[0] = position;
                adapter.setChecked(position);
                adapter.notifyDataSetChanged();
            }
        });

        layout.findViewById(R.id.sure).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.sure, index[0]);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });

        layout.findViewById(R.id.dismiss).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.RIGHT;
        w.setWindowAnimations(R.style.dialogWindowAnim);
        dlg.onWindowAttributesChanged(lp);
        dlg.setCanceledOnTouchOutside(true);
        dlg.setContentView(layout);
        dlg.setCancelable(true);
        return dlg;
    }

    // 修改订单
    public static Dialog ShowUpdateOrderDialog(Context context, final BackCall call) {
        final Dialog dlg = new Dialog(context, R.style.PushUpInDialogThem);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.dialog_update_order_show, null);

        final EditText tv_price = (EditText) layout.findViewById(R.id.tv_price);

        layout.findViewById(R.id.submit_price).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_price.getText().toString().trim().equals("") ||
                        tv_price.getText().toString().trim().length() < 1) {
                    ToastUtils.showToast("请输入正确的价格");
                } else {
                    if (call != null) {
                        call.deal(R.id.submit_price, tv_price.getText().toString());
                        dlg.dismiss();
                    } else {
                        dlg.dismiss();
                    }
                }
            }
        });

        layout.findViewById(R.id.tv_dismiss).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        dlg.onWindowAttributesChanged(lp);
        dlg.setContentView(layout);
        dlg.setCancelable(false);
        return dlg;
    }

    // 选择规格
    public static Dialog ShowSpecDialog(final Context context, final int type, final PartsDetailModel model, final BackCall call) {
        final Dialog dlg = new Dialog(context, R.style.PushUpInDialogThem);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_spec_show, null);
        layout.findViewById(R.id.ll_cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });

        if (type == 0) {// 选规格时 下面显示加入购物车和立即购买
            // 点击屏幕下面的加入购物车和立即购买时 显示确定
            layout.findViewById(R.id.spec_ll).setVisibility(View.VISIBLE);
            layout.findViewById(R.id.add_sure).setVisibility(View.GONE);
        } else {
            layout.findViewById(R.id.spec_ll).setVisibility(View.GONE);
            layout.findViewById(R.id.add_sure).setVisibility(View.VISIBLE);
        }

        ImageLoader.getInstance().displayImage(model.getImg_url(), ((ImageView) layout.findViewById(R.id.spec_img)));
        final TextView spec_num = (TextView) layout.findViewById(R.id.spec_num);
        final TextView spec_price = (TextView) layout.findViewById(R.id.spec_price);
        final TextView spec_kuaidi = (TextView) layout.findViewById(R.id.spec_kuaidi);
        final TextView spec_money = (TextView) layout.findViewById(R.id.spec_money);
        spec_price.setText("¥ " + model.getSpecifications().get(0).getPrice());
        if (model.getSpecifications().get(0).getFreight().equals("0")) {
            spec_kuaidi.setText("快递：免运费");
        } else {
            spec_kuaidi.setText("快递：¥ " + model.getSpecifications().get(0).getFreight());
        }
        spec_money.setText("¥ " + Utils.format(Float.valueOf(model.getSpecifications().get(0).getPrice()) +
                Float.valueOf(model.getSpecifications().get(0).getFreight())));

        final Integer[] isIndex = {0};
        HorizontalListView gridView = (HorizontalListView) layout.findViewById(R.id.parts_spec_grid);
        final PartsSpecAdapter adapter = new PartsSpecAdapter(context);
        adapter.setList(model.getSpecifications());
        adapter.setBackCall(new BackCall() {
            @Override
            public void deal(int which, Object... obj) {
                Integer position = (Integer) obj[0];
                switch (which) {
                    case R.id.text:
                        isIndex[0] = position;
                        spec_price.setText("¥ " + model.getSpecifications().get(position).getPrice());
                        if (model.getSpecifications().get(position).getFreight().equals("0")) {
                            spec_kuaidi.setText("快递：免运费");
                        } else {
                            spec_kuaidi.setText("快递：¥ " + model.getSpecifications().get(position).getFreight());
                        }

                        // 换规格后判断库存
                        if (Integer.valueOf(spec_num.getText().toString()) > Integer.valueOf(model.getSpecifications().get(position).getStock())) {
                            spec_num.setText(model.getSpecifications().get(position).getStock());
                        }
                        spec_money.setText("¥ " + Utils.format((Float.valueOf(model.getSpecifications().get(position).getPrice()) +
                                Float.valueOf(model.getSpecifications().get(position).getFreight())) * Integer.valueOf(spec_num.getText().toString())));
                        adapter.setIsIndex(position);
                        adapter.notifyDataSetChanged();
                        break;
                }
                super.deal(which, obj);
            }
        });
        gridView.setAdapter(adapter);

        spec_num.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Dialog dlg = DialogUtil.ShowNumDialog(context, spec_num.getText().toString(), new BackCall() {
                    public void deal(int which, Object... obj) {
                        String num = (String) obj[0];
                        switch (which) {
                            case R.id.confirm:// 确定
                                // 换规格后判断库存
                                if (Integer.valueOf(num) > Integer.valueOf(model.getSpecifications().get(isIndex[0]).getStock())) {
                                    ToastUtils.showToast("该规格最多" + model.getSpecifications().get(isIndex[0]).getStock() + "件");
                                    spec_num.setText(model.getSpecifications().get(isIndex[0]).getStock());
                                } else {
                                    spec_num.setText(num);
                                }
                                spec_money.setText("¥ " + Utils.format((Float.valueOf(model.getSpecifications().get(isIndex[0]).getPrice()) +
                                        Float.valueOf(model.getSpecifications().get(isIndex[0]).getFreight())) * Integer.valueOf(spec_num.getText().toString())));
                                break;
                        }
                        super.deal(which);
                    }
                });
                dlg.setCanceledOnTouchOutside(true);
                dlg.show();
            }
        });

        layout.findViewById(R.id.spec_jian).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.valueOf(spec_num.getText().toString()) > 1) {
                    spec_num.setText((Integer.valueOf(spec_num.getText().toString()) - 1) + "");
                    spec_money.setText("¥ " + Utils.format((Float.valueOf(model.getSpecifications().get(isIndex[0]).getPrice()) +
                            Float.valueOf(model.getSpecifications().get(isIndex[0]).getFreight())) * Integer.valueOf(spec_num.getText().toString())));
                }
            }
        });

        layout.findViewById(R.id.spec_jia).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Integer.valueOf(spec_num.getText().toString()) < Integer.valueOf(model.getSpecifications().get(isIndex[0]).getStock())) {
                    spec_num.setText((Integer.valueOf(spec_num.getText().toString()) + 1) + "");
                    spec_money.setText("¥ " + Utils.format((Float.valueOf(model.getSpecifications().get(isIndex[0]).getPrice()) +
                            Float.valueOf(model.getSpecifications().get(isIndex[0]).getFreight())) * Integer.valueOf(spec_num.getText().toString())));
                } else {
                    ToastUtils.showToast("库存不足！");
                }
            }
        });

        layout.findViewById(R.id.add_car).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.add_car, isIndex[0], spec_num.getText().toString());
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });
        layout.findViewById(R.id.go_buy).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.go_buy, isIndex[0], spec_num.getText().toString());
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });
        layout.findViewById(R.id.add_sure).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.add_sure, isIndex[0], spec_num.getText().toString());
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        dlg.onWindowAttributesChanged(lp);
        dlg.setContentView(layout);
        dlg.setCancelable(false);
        return dlg;
    }

    public static Dialog ShowNumDialog(final Context context, String num, final BackCall call) {
        final Dialog dlg = new Dialog(context, R.style.PushUpInDialogThem);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.dialog_num_show, null);
        final EditText content = (EditText) layout.findViewById(R.id.content);
        content.setText(num);
        content.setSelection(content.getText().toString().length());// 光标在最后
        layout.findViewById(R.id.confirm).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    if (content.getText().toString().equals("")) {
                        ToastUtils.showToast("请输入数量！");
                    } else {
                        call.deal(R.id.confirm, content.getText().toString());
                        dlg.dismiss();
                    }
                } else {
                    dlg.dismiss();
                }
            }
        });
        layout.findViewById(R.id.cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });
        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.CENTER;
        dlg.onWindowAttributesChanged(lp);
        dlg.setCanceledOnTouchOutside(true);
        dlg.setContentView(layout);
        dlg.setCancelable(false);
        return dlg;
    }

    // 分享
    public static Dialog ShowShareDialog(Context context, final BackCall call) {
        final Dialog dlg = new Dialog(context, R.style.PushUpInDialogThem);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.dialog_share_show, null);

        layout.findViewById(R.id.share_friends).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.share_friends, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });

        layout.findViewById(R.id.share_wxfriends).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.share_wxfriends, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });

        layout.findViewById(R.id.share_sina).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.share_sina, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });

        layout.findViewById(R.id.share_QQfriends).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    call.deal(R.id.share_QQfriends, dlg);
                    dlg.dismiss();
                } else {
                    dlg.dismiss();
                }
            }
        });

        layout.findViewById(R.id.tv_dismiss).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dlg.dismiss();
            }
        });

        Window w = dlg.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        dlg.onWindowAttributesChanged(lp);
        dlg.setContentView(layout);
        dlg.setCancelable(false);
        return dlg;
    }

}
